package com.example.ktapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.ktapplication.R;
import com.example.ktapplication.bean.ArticleDataX;
import com.example.ktapplication.bean.CustomViewsInfo;
import com.stx.xhb.androidx.XBanner;

import java.util.ArrayList;
import java.util.List;

public class HeaderBottomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //item类型
    public static final int ITEM_TYPE_HEADER = 0;
    public static final int ITEM_TYPE_CONTENT = 1;

    private List<ArticleDataX> mArticleList = new ArrayList();
    private List<CustomViewsInfo> mBannerList = new ArrayList();


    private LayoutInflater mLayoutInflater;
    private Context mContext;


    private OnItemClickListener itemClickListener;

    public void updateItem(ArticleDataX dataX) {
        int index = mArticleList.indexOf(dataX);
        if (index != -1) {
            mArticleList.set(index, dataX);
            notifyDataSetChanged();
        }
    }

    public void setItemClickListener(OnItemClickListener clickListener) {
        itemClickListener = clickListener;
    }

    public HeaderBottomAdapter(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }


    public void setData(List<CustomViewsInfo> infoList, List<ArticleDataX> aricleList) {
        mBannerList.addAll(infoList);
        mArticleList.addAll(aricleList);
        notifyDataSetChanged();
    }


    //判断当前item类型
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            //头部View
            return ITEM_TYPE_HEADER;
        } else {
            //内容View
            return ITEM_TYPE_CONTENT;
        }
    }

    //内容 ViewHolder
    public class ContentViewHolder extends RecyclerView.ViewHolder {

        private TextView tvTitle;
        private TextView tvAuthor;
        private TextView tvTime;
        private TextView tvLable;
        private ImageView ivColllect;
        private LinearLayout llBody;

        public ContentViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.txtTitle);
            tvAuthor = itemView.findViewById(R.id.txtAuthor);
            tvTime = itemView.findViewById(R.id.txtTime);
            tvLable = itemView.findViewById(R.id.tvtLable);
            ivColllect = itemView.findViewById(R.id.imgCollect);
            llBody = itemView.findViewById(R.id.llBody);
        }
    }

    //头部 ViewHolder
    public class HeaderViewHolder extends RecyclerView.ViewHolder {

        private XBanner mXBanner;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            // 绑定头部数据和逻辑
            mXBanner = itemView.findViewById(R.id.xbanner);
        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_HEADER) {
            return new HeaderViewHolder(mLayoutInflater
                    .inflate(R.layout.home_recyclerview_header_layout, parent, false));
        }
        if (viewType == ITEM_TYPE_CONTENT) {
            return new ContentViewHolder(mLayoutInflater
                    .inflate(R.layout.home_recyclerview_item_layout, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof HeaderViewHolder) {
            HeaderViewHolder viewHolder = (HeaderViewHolder) holder;
            viewHolder.mXBanner.setBannerData(R.layout.home_banner_layout, mBannerList);
            viewHolder.mXBanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
                @Override
                public void onItemClick(XBanner banner, Object model, View view, int position) {
                    Toast.makeText(mContext, "点击了第" + position + "图片",
                            Toast.LENGTH_SHORT).show();
                }
            });
            viewHolder.mXBanner.loadImage(new XBanner.XBannerAdapter() {
                @Override
                public void loadBanner(XBanner banner, Object model, View view, int position) {
                    TextView textView = view.findViewById(R.id.tv_banner_title);
                    textView.setText(mBannerList.get(position).mContent);
                    ImageView ivContent = view.findViewById(R.id.img_banner);
                    Glide.with(mContext)
                            .load(mBannerList.get(position).mUrl)
                            .into(ivContent);
                }
            });

        }
        if (holder instanceof ContentViewHolder) {
            ArticleDataX bean = mArticleList.get(position - 1);
            ContentViewHolder viewHolder = (ContentViewHolder) holder;
            viewHolder.tvAuthor.setText("作者:" + (bean.getAuthor().isEmpty() ? "未知" : bean.getAuthor()));
            viewHolder.tvTitle.setText(bean.getTitle());
            viewHolder.tvTime.setText("时间:" + (String.valueOf(bean.getNiceShareDate())));
            viewHolder.tvLable.setText("来源:" + bean.getSuperChapterName());
            if (bean.getCollect()) {
                Glide.with(mContext)
                        .load(R.drawable.icon_collect)
                        .into(viewHolder.ivColllect);
            } else {
                Glide.with(mContext)
                        .load(R.drawable.icon_un_collect)
                        .into(viewHolder.ivColllect);
            }
            viewHolder.ivColllect.setOnClickListener(view -> {
                if (itemClickListener != null) {
                    itemClickListener.onCollectClick(bean);
                }
            });
            viewHolder.llBody.setOnClickListener(view -> {
                ///跳转WEB
                if (itemClickListener != null) {
                    itemClickListener.onBodyClick(bean);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (mArticleList.isEmpty()) {
            return 0;
        } else {
            return mArticleList.size() + 1;
        }
    }

    public interface OnItemClickListener {
        void onCollectClick(ArticleDataX item);

        void onBodyClick(ArticleDataX item);
    }

}
